﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using JQuery.WebKit.WebCommon;

namespace JQuery.WebKit.Base
{
    [ParseChildren(false)]
    public class WebControlBase : WebControl, IWebControl
    {
        public new JPage Page
        {
            get { return base.Page as JPage; }
        }

        #region public WebControlBase()
        /// <summary>
        /// Initializes a new instance of the <b>WebControlBase</b> class.
        /// </summary>
        public WebControlBase()
            : this(HtmlTextWriterTag.Div)
        {
        }
        #endregion

        #region public WebControlBase(HtmlTextWriterTag tag)
        /// <summary>
        /// Initializes a new instance of the <b>WebControlBase</b> class.
        /// </summary>
        /// <param name="tag"></param>
        public WebControlBase(HtmlTextWriterTag tag)
            : base(tag)
        {
        }
        #endregion

        #region public WebControlBase(string tag)
        /// <summary>
        /// Initializes a new instance of the <b>WebControlBase</b> class.
        /// </summary>
        /// <param name="tag"></param>
        public WebControlBase(string tag)
            : base(tag)
        {
        }
        #endregion

        #region protected override void Render(System.Web.UI.HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                base.Render(writer);
                return;
            }
            RenderBaseControl(writer);
        }
        #endregion

        public virtual void RenderBaseControl(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
        }

        public virtual void RenderBaseControlChildren(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderChildren(writer);
        }

        public void RegisterJQueryResource(string resourceName)
        {
            RegisterJQueryResource(Page, typeof(JPage), resourceName);
        }

        public void RegisterJSResource(string resourceName)
        {
            RegisterJSResource(Page, typeof(JPage), resourceName);
        }

        public void RegisterCSSResource(string resourceName)
        {
            RegisterCSSResource(Page, typeof(JPage), resourceName);
        }

        #region public void EnsureResource(string resourceName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="resourceName"></param>
        public void RegisterJSResource(JPage page, Type controlType, string resourceName)
        {
            switch (resourceName)
            {
                case Globals.ResourcePaths.JQueryLibraries.JQuery:
                    page.Resources.Add(CommonResources.JQueryCustom);
                    break;
                case Globals.ResourcePaths.JQueryLibraries.JQueryUI:
                    RegisterJSResource(page, controlType, Globals.ResourcePaths.JQueryLibraries.JQuery);
                    page.Resources.Add(CommonResources.JQueryUI);
                    break;
                case Globals.ResourcePaths.JQueryLibraries.JQueryTools:
                    RegisterJSResource(page, controlType, Globals.ResourcePaths.JQueryLibraries.JQuery);
                    page.Resources.Add(CommonResources.JQueryTools);
                    break;
                default:
                    page.Resources.Add(new WebScriptResource(controlType, this.ClientID, ResourceType.Javascript, resourceName) { Dependencies = new WebScriptResource[] { CommonResources.JQuery } });
                    break;
            }
        }
        #endregion

        public void RegisterCSSResource(JPage page, Type controlType, string resourceName)
        {
            HtmlUtil.RegisterResource(controlType, resourceName, ResourceType.Stylesheet);
        }

        public void RegisterJQueryResource(JPage page, Type controlType, string resourceName)
        {
            if (resourceName != Globals.ResourcePaths.JQueryLibraries.JQueryUI)
            {
                page.Resources.Add(new WebScriptResource(controlType, this.ClientID, ResourceType.Javascript, resourceName) { Dependencies = new WebScriptResource[] { CommonResources.JQueryUI } });
            }
        }

        public void AddBodyScript(string script)
        {
            HtmlUtil.RegisterScript(this, script);
        }

        #region IWebControl Members

        public ControlCollection GetChildren()
        {
            return base.Controls;
        }

        #endregion
    }
}
